package com.gx.server.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gx.server.mapper.ExamQuestionMapper;
import com.gx.server.model.domain.ExamErrors;
import com.gx.server.model.domain.ExamQuestion;
import com.gx.server.model.service.ExamErrorsService;
import com.gx.server.model.service.ExamQuestionService;
import jdk.swing.interop.SwingInterOpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Dobby
 * @version 1.0
 */
@Service
public class ExamQuestionServiceImpl extends ServiceImpl<ExamQuestionMapper, ExamQuestion> implements ExamQuestionService {

    @Autowired
    ExamQuestionMapper examQuestionMapper;

    @Autowired
    ExamErrorsService examErrorsService;

    @Override
    public List<ExamQuestion> assign_question(int number, int type, int subject, int question_type) {
        QueryWrapper<ExamQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq("type",type);
        queryWrapper.eq("subject",subject);
        if (queryWrapper!=null){
            queryWrapper.eq("question_type",question_type);
        }

        queryWrapper.last("limit "+number);
        List<ExamQuestion> list = this.list(queryWrapper);

        if (list== null) return null;

        return list;
    }

    @Override
    public List<ExamQuestion> rand_question(Long userId,int number, int type,int subject) {
        // 创建LambdaQueryWrapper对象
        QueryWrapper<ExamQuestion> queryWrapper = new QueryWrapper<>();
        // 设置查询条件
        queryWrapper.select("*");

        queryWrapper.eq("subject", subject);
        queryWrapper.eq("type",type);

        // 设置随机抽取题目
        queryWrapper.last("ORDER BY RAND() LIMIT "+number);

        // 执行查询 随机的题库
        List<ExamQuestion> list = this.list(queryWrapper);

        //当前number 的30%是多少 需要从错题库中提取多少题
        int extract = (int) Math.round(number*0.3);
        System.out.println("extract-->");
        System.out.println(extract);

        //把错题的权重高的错题 提取占number 的30%（如果错题 不能填满30% 则 能有多少填多少
        LambdaQueryWrapper<ExamErrors> queryWrapper_error = new LambdaQueryWrapper<>();
        queryWrapper_error.eq(ExamErrors::getUserId,userId);
        queryWrapper_error.eq(ExamErrors::getWeight,1);

        int count = examErrorsService.count(queryWrapper_error);

        if (count<extract) extract = count;
        if (count==0) return list;

//        错题的题库
        List<ExamQuestion> examQuestions_errors = new ArrayList<>();
        List<ExamErrors> examErrorsList = examErrorsService.list(queryWrapper_error);
        for (ExamErrors examErrors : examErrorsList) {
            examQuestions_errors.add(this.sing_question(examErrors.getQuestionId()));
        }

        //覆盖
        for (int i = 0; i < examQuestions_errors.size(); i++) {
            list.set(i, examQuestions_errors.get(i));
        }

        return list;
    }

    @Override
    public List<ExamQuestion> all_question(int subject) {
        QueryWrapper<ExamQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq("subject",subject);
        List<ExamQuestion> examQuestions = examQuestionMapper.selectList(queryWrapper);
        return examQuestions;
    }

    @Override
    public Integer number_question() {
        int count = this.count();
        return count;
    }

    @Override
    public ExamQuestion sing_question(int questionId) {
        QueryWrapper<ExamQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*");
        queryWrapper.eq("id",questionId);

        return this.getOne(queryWrapper);
    }


    @Override
    public List<ExamQuestion> all_Errors(Long id) {
        LambdaQueryWrapper<ExamErrors> examCollectLambdaQueryWrapper = new LambdaQueryWrapper<ExamErrors>();
        examCollectLambdaQueryWrapper.eq(ExamErrors::getUserId,id);
        List<ExamErrors> list = examErrorsService.list(examCollectLambdaQueryWrapper);

        //存放题目容器
        List<ExamQuestion> examQuestions = new ArrayList<>();
        for (ExamErrors examErrors : list) {
            QueryWrapper<ExamQuestion> queryWrapper= new QueryWrapper<>();
            queryWrapper.select("*");
            queryWrapper.eq("id",examErrors.getQuestionId());
            ExamQuestion one = this.getOne(queryWrapper);
            examQuestions.add(one);
        }

        return examQuestions;
    }
}
